﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using Microsoft.Windows.Controls;

using InfFront.InfoViews.Base;
using InfFront.UICore;

namespace InfFront.InfoViews.WPF
{
    /// <summary>
    /// Interaction logic for ListWithToolbarFrame.xaml
    /// </summary>
    public partial class ListWithToolbarFrame : BaseSourceContentFrame
    {
        #region Private Variables
        private InfoViewEntry curEntry = null;
        private bool rebuilding = false;
        #endregion

        #region Construction/Initialization
        public ListWithToolbarFrame(InfoViewSource source)
        {
            InitializeComponent(); // TODO: design dynamic context menu which is rebubuilt every time it is opened aggregating all context-aware options
            Source = source;
        }
        #endregion

        public BaseListWithToolbarContentFrame CurrentContentFrame
        {
            get 
            { 
                return (BaseListWithToolbarContentFrame)content.Content; 
            }
            set
            {
                if (content.Content != null)
                {
                    var oldValue = (BaseListWithToolbarContentFrame)content.Content;
                    oldValue.Owner = null;
                    oldValue.Source = Source;
                }
                content.Content = value;
                if (value != null)
                {
                    value.Owner = this;
                    value.Source = Source;
                }
            }
        }

        public override void Rebuild()
        {
            rebuilding = true;
            try
            {
                commandsControlList.ItemsSource = null;
                curEntry = null;
                CurrentContentFrame = null;
                
                if (Source == null || Source.IsDummy)
                {
                    return;
                }

                commandsControlList.ItemsSource = Source.Commands;

                switch (ListStyle)
                {
                    case ListWithToolbarFrameStyle.Grid:
                        CurrentContentFrame = new DataGridFrame();
                        break;

                    case ListWithToolbarFrameStyle.Plot:
                        CurrentContentFrame = new PlotFrame();
                        break;

                    case ListWithToolbarFrameStyle.Icons:
                        CurrentContentFrame = new ListViewFrame();
                        break;

                    default:
                        throw new NotSupportedException(string.Format("Unknown ListWithToolbarFrameStyle: {0}", ListStyle));
                }
                UpdateWithNewCurrentEntry(Source.GetFirstEntry());
            }
            finally
            {
                rebuilding = false;
            }
        }

        public override void Refresh()
        {
            if (rebuilding)
                return;

            if (Source == null || Source.IsDummy)
                return;

            if (CurrentContentFrame != null)
                CurrentContentFrame.Refresh();

            UpdateWithNewCurrentEntry(Source.GetFirstEntry());
        }

        private void CommandButton_Click(object sender, RoutedEventArgs e)
        {
            var cmd = ((FrameworkElement)e.Source).DataContext as InfoViewCommand;
            if (cmd == null)
            {
                Debug.Assert(false, "No InfoViewCommand found");
                return;
            }
            var args = new InfoViewCommand.ExecutionArgs();
            args.CurrentEntry = curEntry;
            cmd.Execute(args);
        }

        internal void UpdateWithNewCurrentEntry(InfoViewEntry entry)
        {
            if (curEntry != null)
                curEntry.Dispose();
            curEntry = entry;
            if (CurrentContentFrame != null)
                CurrentContentFrame.CurrentEntry = entry;
            int i = 0;
            foreach (bool cmdEnabled in Source.CheckCommandsEnabled(entry))
            {
                var cmd = (InfoViewCommand)commandsControlList.Items[i++];
                cmd.IsEnabled = cmdEnabled;
            }
        }

        private void PreferencesMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var globals = AppGlobals;
            if (globals == null)
                return;
            globals.MainFrame.ShowPreferences(Source);
        }

        private ListWithToolbarFrameStyle ListStyle
        {
            get { return (ListWithToolbarFrameStyle)Source.VisualSettings.GetValue("ListWithToolbarFrameStyle"); }
            set { Source.VisualSettings.SetValue("ListWithToolbarFrameStyle", value); }
        }

        private void ListStyleMenuItem_Clicked(object sender, RoutedEventArgs e)
        {
            var mi = (MenuItem)sender;
            var newListStyle = (ListWithToolbarFrameStyle)mi.Tag;
            if (ListStyle != newListStyle)
            {
                ListStyle = (ListWithToolbarFrameStyle)mi.Tag;
                foreach (MenuItem styleMI in viewAsSubmenu.Items)
                {
                    if (styleMI != mi)
                        styleMI.IsChecked = false;
                }
                Dispatcher.BeginInvoke(new AsyncCallMethod(Rebuild));
            }
            e.Handled = true;
        }
    }
}

